package com.xdjmall.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xdjmall.common.Constance;
import com.xdjmall.dao.GoodsDao;
import com.xdjmall.dao.IndexConfigDao;
import com.xdjmall.dao.impl.GoodsDaoImpl;
import com.xdjmall.dao.impl.IndexConfigDaoImpl;
import com.xdjmall.entity.Goods;
import com.xdjmall.entity.IndexConfig;
import com.xdjmall.service.IndexConfigService;
import com.xdjmall.servlet.vo.IndexGoodsVO;

public class IndexConfigServiceImpl implements IndexConfigService {

	private IndexConfigDao dao = new IndexConfigDaoImpl();
	private GoodsDao gdao = new GoodsDaoImpl();

	@Override
	public List<IndexGoodsVO> getIndexGoods(Integer type, Integer num) throws SQLException {
		List<IndexConfig> ics = dao.selectIndexConfigsByTypeAndNum(type,num);
		// 倒腾
		List<IndexGoodsVO> igvs = new ArrayList<>();
		ics.forEach(ic -> {
			try {
				Goods goods = gdao.selectById(ic.getGoodsId());
				IndexGoodsVO igv = new IndexGoodsVO();
				igv.setGoodsId(goods.getGoodsId());
				igv.setGoodsName(goods.getGoodsName());
				igv.setGoodsCoverImg(goods.getGoodsCoverImg());
				igv.setSellingPrice(goods.getSellingPrice());
				igv.setTag(goods.getTag());
				igv.setGoodsIntro(goods.getGoodsIntro());
				igvs.add(igv);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		});
		return igvs.isEmpty() ? Collections.emptyList() : igvs;
	}

}
